<!-- 轮案组信息 -->
<template>
  <div class="lazxx">
    <!-- 监管团队信息--satrt -->
    <div class="jgtdxx" v-loading="jgtdxxLoading">
      <div class="section">
        <span>
          <img src="@/assets/img/jdmh/11.png" alt="" />
          监管团队信息
        </span>
        <div class="bnts">
          <el-button
            type="text"
            class="btn"
            v-if="jgtdxx.tdbh"
            @click="saveJgtdxx"
          >
            <i class="el-icon-plus"></i>
            保存
          </el-button>
          <el-button
            style="margin-left: 0"
            type="text"
            class="btn"
            v-if="jgtdxx.tdbh"
            @click="delJgtdxx"
          >
            <i class="el-icon-delete"></i>
            删除
          </el-button>
        </div>
      </div>
      <div class="content">
        <el-descriptions :column="2" border>
          <el-descriptions-item>
            <template slot="label">监管团队名称</template>
            <el-input v-model="jgtdxx.tdmc" />
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">负责人</template>
            <el-select
              v-model="jgtdxx.tdfzrbm"
              filterable
              @change="tdfzrbmChange"
            >
              <el-option
                v-for="item in tableData"
                :key="item.rybm"
                :label="item.rymc"
                :value="item.rybm"
              >
              </el-option>
            </el-select>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">参与轮案</template>
            <el-radio-group v-model="jgtdxx.sfcyla">
              <el-radio label="Y">是</el-radio>
              <el-radio label="N">否</el-radio>
            </el-radio-group>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">参与自动轮案</template>
            <el-radio-group v-model="jgtdxx.sfzdcyla">
              <el-radio label="Y">是</el-radio>
              <el-radio label="N">否</el-radio>
            </el-radio-group>
          </el-descriptions-item>
          <el-descriptions-item>
            <template slot="label">互斥规则</template>
            <el-radio-group v-model="jgtdxx.sfhc">
              <el-radio label="Y">是</el-radio>
              <el-radio label="N">否</el-radio>
            </el-radio-group>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </div>
    <!-- 监管团队信息--end -->
    <!-- 团队成员--start -->
    <div class="tdcy" v-loading="tdryLoading">
      <div class="section">
        <span>
          <img src="@/assets/img/jdmh/11.png" alt="" />
          团队成员
        </span>
        <el-button type="text" class="btn" @click="handleCy()" v-if="tdbh">
          <i class="el-icon-plus"></i>
          添加成员
        </el-button>
      </div>
      <div class="content">
        <el-table :data="tableData" border stripe height="100%">
          <el-table-column
            v-for="item in tableHead"
            :prop="item.prop"
            :key="item.prop"
            :label="item.label"
          >
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-link type="primary" @click="handleCy(scope.row)"
                >修改</el-link
              >
              <el-link type="primary" @click="delTdry(scope.row)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 团队成员--end -->
    <!-- 编辑团队成员 -->
    <editcy :tdbh="tdbh" ref="editcyRef" @refresh="init" />
  </div>
</template>
<script>
import tdxxService from '@/api/tdxx.js'
import tdryService from '@/api/tdry.js'
import editcy from '../dialogs/edit-cy.vue'
export default {
  name: 'lazxx',
  components: {
    editcy
  },
  props: {
    tdbh: String
  },
  data () {
    return {
      jgtdxxLoading: false,
      tdryLoading: false,
      // 监管团队信息
      jgtdxx: {},
      // 团队成员信息
      tableHead: [
        { label: '角色', prop: 'jsmc' },
        { label: '姓名', prop: 'rymc' },
        { label: '工号', prop: 'rybm' },
        { label: '在职状态', prop: 'ryztmc' },
        { label: '联系电话', prop: 'lxdh' }
      ],
      tableData: []
    }
  },
  watch: {
    tdbh: {
      handler (val) {
        this.jgtdxx = {}
        this.tableData = []
        if (val) {
          this.init(val)
        }
      },
      immediate: true
    }
  },
  methods: {
    init (tdbh) {
      this.getTdxx(tdbh)
      this.queryTdry(tdbh)
    },
    /** 获取监管团队信息 */
    getTdxx (tdbh) {
      this.jgtdxxLoading = true
      const params = {
        tdbh
      }
      tdxxService
        .getTdxx(params)
        .then(res => {
          this.jgtdxx = res
        })
        .catch(err => {
          !err.isError &&
            this.$message({ type: 'warning', message: err.message })
        })
        .finally(() => {
          this.jgtdxxLoading = false
        })
    },
    /** 保存监管团队信息 */
    saveJgtdxx () {
      const { dwbm, dwmc } = this.$store.state.personInfo
      const params = {
        ...this.jgtdxx,
        dwbm,
        dwmc
      }
      tdxxService
        .saveTdxx(params)
        .then(res => {
          this.$emit('refresh')
          this.$message({ type: 'success', message: '保存成功' })
        })
        .catch(err => {
          !err.isError &&
            this.$message({ type: 'warning', message: err.message })
        })
    },

    /** 删除团队信息 */
    delJgtdxx () {
      this.$confirm('是否删除当前团队？', '删除提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        tdxxService
          .deleteTdxx({
            tdbh: this.tdbh
          })
          .then(res => {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.$emit('refresh')
          })
          .catch(err => {
            !err.isError &&
              this.$message({ type: 'warning', message: err.message })
          })
      })
    },

    /** 团队负责人下拉改变 */
    tdfzrbmChange (rybm) {
      this.jgtdxx.tdfzrmc = this.tableData.find(
        item => item.rybm === rybm
      )?.rymc
    },

    /** 获取团队人员信息 */
    queryTdry (tdbh) {
      this.tdryLoading = true
      tdryService
        .queryTdry({
          tdbh
        })
        .then(res => {
          this.tableData = res
        })
        .catch(err => {
          !err.isError &&
            this.$message({ type: 'warning', message: err.message })
        })
        .finally(() => {
          this.tdryLoading = false
        })
    },
    /** 新增/编辑团队成员 */
    handleCy (data) {
      this.$refs.editcyRef.openDialog(data)
    },

    /** 团队人员操作 */
    // 删除团队人员
    delTdry (row) {
      this.$confirm('是否删除当前人员？', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        tdryService
          .deleteTdry({
            rybm: row.rybm,
            tdbh: row.tdbh
          })
          .then(res => {
            this.$message({ type: 'success', message: '删除成功' })
            this.queryTdry(this.tdbh)
          })
          .catch(err => {
            !err.isError &&
              this.$message({ type: 'warning', message: err.message })
          })
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.lazxx {
  width: 100%;
  height: 100%;
  padding: 10px;
  display: flex;
  flex-direction: column;
  > div {
    display: flex;
    flex-direction: column;
    .section {
      height: 40px;
      background-color: #c8d7f8;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 10px;
      color: #165ac6;
      span {
        display: flex;
        align-items: center;
        img {
          margin-right: 10px;
          width: 20px;
        }
      }
      .btn {
        background-color: transparent;
        border: none;
        color: #165ac6;
        min-width: 75px;
      }
    }
    .content {
      flex: 1;
      height: 0;
      border: 1px solid #c8d7f8;
      border-top: none;
    }
  }
  .jgtdxx {
    margin-bottom: 10px;
    .content {
      height: 185px;
      padding: 10px;
      .el-descriptions {
        /deep/ .el-descriptions-row {
          td {
            width: 25%;
          }
          .el-descriptions-item__label {
            text-align: right;
          }
        }
      }
    }
  }
  .tdcy {
    flex: 1;
    height: 0;
    display: flex;
    flex-direction: column;
    .content {
      padding: 10px;
      flex: 1;
      height: 0;
    }
  }
}
</style>
